import { getAllCourseList } from '@/views/api/course/allCourse'
import userManageApi from '@/views/api/system/user/userManage'
import courseApi from '@/views/api/course/course'
import { mapGetters } from 'vuex'

export default {
    data() {
        return {
            imageUrl: '',
            isFold: false,
            user: {},
            courseList: [],
            teacherList: [],
            list: []
        }
    },
    created() {
        this.init()
    },
    mounted() {
        this.$store.commit('asideMenu/SET_ASIDE_INDEX', "3")
    },
    computed: {
        ...mapGetters(['userInfo']),
    },
    methods: {
        init() {
            this.fetchData()
        },
        async fetchData() {
            await userManageApi.getTeacherList().then(response => {
                this.teacherList = response.data
            })
            this.getCourse()
        },
        getCourse() {
            getAllCourseList().then(response => {
                this.courseList = response.data
                this.list = this.courseList.filter(item => {
                    if (this.userInfo.courseIds.includes(item.id)) {
                        item.teacherName = this.teacherList.find(value => value.id === item.teacherId).username;
                        return item
                    }
                })
            })
        },
        deselect(row) {
            this.$confirm('是否确认退选此课程？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                userManageApi.deselect(row.id, this.userInfo.id).then(response => {
                    this.$store.commit('user/SET_USER_INFO', response.data)
                })
                courseApi.deselectCourse(row.id, this.userInfo.id).then(response => {
                    this.$message({
                        message: '退选成功',
                        type: 'success'
                    })
                    this.getCourse()
                })
            })

        },
        clickToFold() {
            this.isFold = true
        },
        clickToUnfold() {
            this.isFold = false
        },
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}
